#include "stdafx.h"
#include "Dataserver.h"
#include "WzItemSerial.h"
#include "MainFrm.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif

CWzItemSerial gWzItemSerial;

CWzItemSerial::CWzItemSerial(): m_dwItemSerial(0), m_dwLastSerial(0)
{
	InitializeCriticalSection(&this->m_csItemSerial);
}

CWzItemSerial::~CWzItemSerial()
{
	DeleteCriticalSection(&this->m_csItemSerial);
}

int CWzItemSerial::MakeSerial()
{
	if (gGSDbSet.GetItemSerial(this->m_dwItemSerial, 3000) == FALSE)
	{
		return FALSE;
	}

	this->m_dwLastSerial = this->m_dwItemSerial + 3000;
	return 1;
}

DWORD CWzItemSerial::GetSerial()
{
	EnterCriticalSection(&this->m_csItemSerial);

	if (this->m_dwItemSerial >= this->m_dwLastSerial)
	{
		this->MakeSerial();
	}

	DWORD retserial = this->m_dwItemSerial;
	this->m_dwItemSerial++;
	LeaveCriticalSection(&this->m_csItemSerial);
	return retserial;
}